package com.healthcloud.controller.modules.user;

import com.healthcloud.entity.*;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.service.SysUserService;
import com.healthcloud.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author zj
 */
@Api(value = "用户管理接口")
@Controller
@RequestMapping(value = "/user")
public class UserController {
	
	@Autowired
	private SysUserService sysUserService;

    @Autowired
    private HlHospitalService hlHospitalService;

    @ApiOperation(value = "基本信息页面")
    @RequestMapping(value = "/toUserInfo",method=RequestMethod.GET)
    public String toUserInfo(){
        return "base/user/userInfo";
    }

    @ApiOperation(value = "获取基本信息")
	@RequestMapping(value = "/getUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public ApiRes getUserInfo(){
        SysUserEntity user = ShiroUtils.getUserEntity();

        List<HlHospitalEntity> hospitals = hlHospitalService.getHospitalsByUserId(user.getId());

        Map<String, Object> data = new HashMap<>();
        data.put("userImage", user.getUserImage());
        data.put("username", user.getUsername());
        data.put("roles", user.getRoleList());
        data.put("realName", user.getRealName());
        data.put("mobile", user.getMobile());
        data.put("hospitalName", "");

        if (hospitals!=null && hospitals.size()>0){
            data.put("hospitalName", hospitals.get(0).getHospName());
        }
        return new ApiRes(data);
    }

    @ApiOperation(value = "修改密码页面")
	@RequestMapping(value = "/toUpdatePassword",method=RequestMethod.GET)
    public String toUpdatePassword(){
        return "base/user/updatePassword";
    }

    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes updatePassword(@ApiParam(value = "原密码", name = "oldPassword")
                                  @RequestParam(value = "oldPassword")  String oldPassword,
                                 @ApiParam(value = "新密码", name = "newPassword")
                                  @RequestParam(value = "newPassword")  String newPassword) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if (!Objects.equals(user.getPassword(), EncryptionUtils.encryptionMd5(oldPassword))){
            return new ApiRes(false,null,null,"原密码不正确");
        }

        sysUserService.updatePassword(user.getId(), EncryptionUtils.encryptionMd5(oldPassword)
                , EncryptionUtils.encryptionMd5(newPassword));
        return ApiRes.SUCCESS;
    }

}
